package leetcode.ge100;

import leetcode.lt100.TreeNode;


class Solution_110 {


    public static void main(String[] args) {
    }


    public boolean isBalanced(TreeNode root) {
        return deep(root) != -1;
    }

    private int deep(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = deep(root.left);
        if (left == -1) {
            return -1;
        }
        int right = deep(root.right);
        if (right == -1) {
            return -1;
        }
        if (left < right - 1 || left > right + 1) {
            return -1;
        }
        return Math.max(left, right) + 1;
    }


}